﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Web.Mvc;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers
{
    /// <summary>
    /// 控制器基类
    /// </summary>
    public class BaseController : Controller
    {
        /// <summary>
        /// 服务访问对象
        /// </summary>
        protected static ServiceReference1.Service1Client SRS = new ServiceReference1.Service1Client(); // 创建服务器访问对象
        /// <summary>
        /// 验证码
        /// </summary>
        protected static string YZM = GetYZM._YZM;
        /// <summary>
        /// 操作返回标记
        /// </summary>
        protected int TAG = -1;
        /// <summary>
        /// Json数据转化为对象
        /// </summary>
        /// <param name="jsonData">要转化的Json数据</param>
        /// <param name="obj">对象的类型</param>
        /// <returns>转换后的对象</returns>
        public static object JsonToObject(string jsonData, object obj)
        {
            return JsonConvert.DeserializeObject(jsonData, obj.GetType());
        }
        /// <summary>
        /// 对象转换为Json数据
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <returns>转换后的字符串数据</returns>
        protected static string ObjectToJson(object obj)
        {
            return JsonConvert.SerializeObject(obj); ;
        }
        /// <summary>
        /// 获取积分转换单位
        /// </summary>
        /// <param name="model">List<Model.View_Order_Int></param>
        /// <returns>List<SelectListItem></returns>
        protected static List<SelectListItem> GetIntUnList(Model.View_Order_Int model)
        {
            #region 积分转换单位
            List<SelectListItem> IntUnList = new List<SelectListItem>();

            SelectListItem IntUnItem_F = new SelectListItem();
            IntUnItem_F.Text = "分";
            IntUnItem_F.Value = "1";
            if (model != null && IntUnItem_F.Value.Equals(model.IntUn))
            {
                IntUnItem_F.Selected = true;
            }
            IntUnList.Add(IntUnItem_F);

            SelectListItem IntUnItem_J = new SelectListItem();
            IntUnItem_J.Text = "角";
            IntUnItem_J.Value = "2";
            if (model != null && IntUnItem_J.Value.Equals(model.IntUn))
            {
                IntUnItem_J.Selected = true;
            }
            IntUnList.Add(IntUnItem_J);

            SelectListItem IntUnItem_Y = new SelectListItem();
            IntUnItem_Y.Text = "元";
            IntUnItem_Y.Value = "3";
            if (model != null && IntUnItem_Y.Value.Equals(model.IntUn))
            {
                IntUnItem_Y.Selected = true;
            }
            IntUnList.Add(IntUnItem_Y);

            #endregion

            return IntUnList;
        }
        /// <summary>
        /// 获取转换金额处理
        /// </summary>
        /// <param name="model">List<Model.View_Order_Int></param>
        /// <returns>List<SelectListItem></returns>
        protected static List<SelectListItem> GetIntYList(Model.View_Order_Int model)
        {
            #region 转换金额处理
            List<SelectListItem> IntYList = new List<SelectListItem>();

            SelectListItem IntYItem_1 = new SelectListItem();
            IntYItem_1.Text = "非整数时，小数点忽略";
            IntYItem_1.Value = "1";
            if (model != null && IntYItem_1.Value.Equals(model.IntY))
            {
                IntYItem_1.Selected = true;
            }
            IntYList.Add(IntYItem_1);

            SelectListItem IntYItem_2 = new SelectListItem();
            IntYItem_2.Text = "非整数时，小数点四舍五入";
            IntYItem_2.Value = "2";
            if (model != null && IntYItem_2.Value.Equals(model.IntY))
            {
                IntYItem_2.Selected = true;
            }
            IntYList.Add(IntYItem_2);

            SelectListItem IntYItem_3 = new SelectListItem();
            IntYItem_3.Text = "非整数时，小数点进一";
            IntYItem_3.Value = "3";
            if (model != null && IntYItem_3.Value.Equals(model.IntY))
            {
                IntYItem_3.Selected = true;
            }
            IntYList.Add(IntYItem_3);

            #endregion

            return IntYList;
        }
        /// <summary>
        /// 获取是否打开
        /// </summary>
        /// <param name="model">List<Model.View_Order_Int></param>
        /// <returns>List<SelectListItem></returns>
        protected static List<SelectListItem> GetIsOpenList(Model.View_Order_Int model)
        {
            #region 是否打开
            List<SelectListItem> IsOpenList = new List<SelectListItem>();

            SelectListItem IsOpenItem_0 = new SelectListItem();
            IsOpenItem_0.Text = "否";
            IsOpenItem_0.Value = "0";
            if (model != null && IsOpenItem_0.Value.Equals(model.IsOpen))
            {
                IsOpenItem_0.Selected = true;
            }
            IsOpenList.Add(IsOpenItem_0);

            SelectListItem IsOpenItem_1 = new SelectListItem();
            IsOpenItem_1.Text = "是";
            IsOpenItem_1.Value = "1";
            if (model != null && IsOpenItem_1.Value.Equals(model.IsOpen))
            {
                IsOpenItem_1.Selected = true;
            }
            IsOpenList.Add(IsOpenItem_1);

            #endregion

            return IsOpenList;
        }
        /// <summary>
        /// 将正确的JSON列表数据去掉多余的字符串
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        protected string ResultList(string result)
        {
            string value = "";
            if (!string.IsNullOrEmpty(result))
            {
                result = result.Replace(" ", "").Trim();
                JObject jo = JObject.Parse(result);
                string length = jo["total"].ToString();
                value = result.Replace("{\"BS\":\"1\",\"Msg\":\"查询成功！\",\"total\":0,\"rows\":", "");
                value = value.Substring(0, value.Length - 1);
            }
            return value;
        }
        /// <summary>
        /// 根据ID获取字典数据
        /// </summary>
        /// <param name="DictID"></param>
        /// <returns></returns>
        protected List<SelectListItem> GetDicForDictID(string DictID)
        {
            // 获取字典
            List<Basic_Dict_T> dicList = AppDictionary.GetDicList();
            // 账号类型
            List<SelectListItem> itemList = new List<SelectListItem>();
            // 
            foreach (Basic_Dict_T dic in dicList)
            {
                if (dic.DictId == int.Parse(DictID))
                {
                    SelectListItem sli = new SelectListItem();
                    sli.Text = dic.DictName;
                    sli.Value = dic.DictKey;
                    // sli.Selected = false;
                    itemList.Add(sli);
                }
            }
            return itemList;
        }
        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <returns></returns>
        protected List<Basic_Department_T> GetDepList(Model.View_OperUser operUser)
        {
            List<Basic_Department_T> list = new List<Basic_Department_T>();
            if (operUser != null)
            {
                // 获取服务上的数据
                string result = new ZHHQSERVICE.Service1Client().GetDepatMent(operUser.OperID, "@ZBUSER", YZM, 1);
                if (!string.IsNullOrEmpty(result) && result.IndexOf("\"BS\":\"1\"") > -1)
                {
                    result = ResultList(result);
                    list = JsonConvert.DeserializeObject(result, list.GetType()) as List<Basic_Department_T>;
                }
            }
            return list;
        }

     
    }
}
